package com.pdsu.alumni.controller.systemManager;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pdsu.alumni.common.result.Result;
import com.pdsu.alumni.pojo.Resource;
import com.pdsu.alumni.service.ResourceService;
import com.pdsu.alumni.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.*;

@Api(tags = "权限控制")
@RestController
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "获取所有权限")
    @GetMapping("getList")
    @RequiresAuthentication
    @RequiresPermissions("sys:menu")
    @ResponseBody
    @CrossOrigin
    public Result<?> getList() {
        List<Resource> list = resourceService.list();
        return new Result<>().success().put(list);
    }

    @ApiOperation(value = "删除一个菜单")
    @PostMapping("del")
    @RequiresAuthentication
    @RequiresPermissions("sys:menu:delete")
    @ResponseBody
    @CrossOrigin
    public Result<?> del(@RequestBody Map map) {
        resourceService.removeById((Serializable) map.get("id"));

        return new Result<>().success();
    }

    @ApiOperation(value = "添加菜单")
    @PostMapping("add")
    @RequiresAuthentication
    @RequiresPermissions("sys:menu:add")
    @ResponseBody
    @CrossOrigin
    public Result<?> add(@RequestBody Map map) {
        Object res = map.get("resource");
        Resource resource = JSON.parseObject(JSON.toJSONString(res), Resource.class);
        String name = resource.getResourceName();
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        wrapper.eq("resource_name", name);
        int count = resourceService.count(wrapper);
        if (count > 0) {
            return new Result<>().error(500, "该菜单已存在！");
        }
        resource.setResourceDelFlag(0);
        resource.setResourceCreatetime(DateUtil.formatTime(new Date()));
        resource.setResourceUpdatetime(DateUtil.formatTime(new Date()));
        resourceService.save(resource);
        return new Result<>().success();
    }

    @ApiOperation(value = "修改菜单")
    @PostMapping("update")
    @RequiresAuthentication
    @RequiresPermissions("sys:menu:edit")
    @ResponseBody
    @CrossOrigin
    public Result<?> update(@RequestBody Map map) {
        Object res = map.get("resource");
        Resource resource = JSON.parseObject(JSON.toJSONString(res), Resource.class);
        resource.setResourceUpdatetime(DateUtil.formatTime(new Date()));
        resourceService.updateById(resource);
        return new Result<>().success();
    }


    @ApiOperation(value = "获取父级菜单下拉框")
    @GetMapping("getCascader")
    @RequiresAuthentication
    @RequiresPermissions("sys:menu")
    @ResponseBody
    @CrossOrigin
    public Result<?> getCascader() {
        List<Resource> list = resourceService.list();
        List<Map> cas = new ArrayList<>();
        Map mainPerm = new HashMap();
        mainPerm.put("label", "主菜单");
        mainPerm.put("value", "0");
        cas.add(mainPerm);
        for (Resource resource : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("label", resource.getResourceName());
            map.put("value", resource.getResourceId());
            cas.add(map);
            if (resource.getChildren() != null && resource.getChildren().size() > 0) {
                List<Map> child = getCasChildren(resource.getChildren());
                map.put("children", child);
            }
        }
        return new Result<>().success().put(cas);
    }

    private List<Map> getCasChildren(List<Resource> resources) {
        List<Map> cas = new ArrayList<>();
        for (Resource resource : resources) {
            Map<String, Object> map = new HashMap<>();
            map.put("label", resource.getResourceName());
            map.put("value", resource.getResourceId());
            cas.add(map);
            if (resource.getChildren() != null && resource.getChildren().size() > 0) {
                List<Map> child = getCasChildren(resource.getChildren());
                map.put("children", child);
            }
        }
        return cas;
    }
}
